<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Bannerdetail_model extends Model
{
	private $_host;
	private $_dbname;
	private $_username;
	private $_password;
	private $_port;
	private $_folderImageStore = 'uploads/images/';
	
	function Bannerdetail_model()
	{
		parent::Model();	
        $host = $this->db->hostname;
		$arrHost = explode(':',$host);
		$temp =$this->_host = trim($arrHost[0]);
		$this->_dbname = $this->db->database;
		$this->_username = $this->db->username;
		$this->_password = $this->db->password;
		$this->_port = $this->db->port;
	}
    
    function selfserving_get_banner_info($bannerid)
    {
        $data = array();
        $data['info']=array();
        $data['category']=array();
        $iconn = $this->db->conn_id;
		mysqli_multi_query($iconn, "CALL selfserving_get_banner_info('$bannerid');") or show_custom_error(mysqli_error($iconn));
        $result = mysqli_store_result($iconn);
        $CI = &get_instance();
        $CI->load->library('mycommon');
        while($row = mysqli_fetch_array($result))
		{
            $row['filepath'] = '';
			$createdTime = date($row['createtime']);
			$createdTimeInfo = getdate(strtotime($createdTime));
			$yearFolder = $createdTimeInfo['year'];
			$monthFolder = $createdTimeInfo['mon'];
			$monthFolder = ($monthFolder < 10) ? "0" . $monthFolder : $monthFolder;
			$row['filepath'] = $CI->mycommon->getBannerImagPath($yearFolder, $monthFolder, $row['filename']);
			$data['info'][] = $row;
		}
        mysqli_free_result($result);
		mysqli_next_result($iconn);
        $result = mysqli_store_result($iconn);
        while($row1 = mysqli_fetch_array($result))
		{
			$data['category'][] = $row1;
		}
        mysqli_free_result($result);
		if(mysqli_more_results($iconn))
		{
			mysqli_next_result($iconn);
		}
		return $data;
    }
    
    function selfserving_get_all_by_banner($bannerid,$fromdate,$todate)
	{
        $data=array();
        $data['banner']=array();
        $data['total']=array();
        $iconn = $this->db->conn_id;
        mysqli_multi_query($iconn, "CALL selfserving_get_all_by_banner('$bannerid', '$fromdate', '$todate');") or show_custom_error(mysqli_error($iconn));		
        $result = mysqli_store_result($iconn);
        while($row = mysqli_fetch_array($result))
		{
            $row['clickdate']=date('d/m/Y',strtotime($row['clickdate']));
			$data['banner'][] = $row;
		}
        mysqli_free_result($result);
		mysqli_next_result($iconn);
        $result = mysqli_store_result($iconn);
        while($row1 = mysqli_fetch_array($result))
		{
			$data['total'][] = $row1;
		}
        mysqli_free_result($result);
		if(mysqli_more_results($iconn))
		{
			mysqli_next_result($iconn);
		}
		return $data;
	}
    
    function selfserving_get_banner_chart($bannerid,$fromdate,$todate,$type)
	{
        $data=array();
        $data['ban1']=array();
        $data['ban2']=array();
        $iconn = $this->db->conn_id;
        mysqli_multi_query($iconn, "CALL selfserving_get_banner_chart('$bannerid', '$fromdate', '$todate', '$type');") or show_custom_error(mysqli_error($iconn));		
        $result = mysqli_store_result($iconn);
        while($row = mysqli_fetch_array($result))
		{
			$data['ban1'][] = $row;
		}
        mysqli_free_result($result);
		mysqli_next_result($iconn);
        $result = mysqli_store_result($iconn);
        while($row1 = mysqli_fetch_array($result))
		{
			$data['ban2'][] = $row1;
		}
        mysqli_free_result($result);
		if(mysqli_more_results($iconn))
		{
			mysqli_next_result($iconn);
		}
		return $data;
	}
    function selfserving_banner_update_inline($bannerid,$val,$type)
    {
        $userid=$this->session->userdata('uid');
        $iconn = $this->db->conn_id;
        $result =mysqli_multi_query($iconn, "CALL selfserving_banner_update_inline('$bannerid', '$val','$type','$userid');");
		if(mysqli_more_results($iconn))
		{
			mysqli_next_result($iconn);
		}
		return $result;
    }
    function selfserving_get_all_categories()
    {
        $data = array();
        $iconn = $this->db->conn_id;
        mysqli_multi_query($iconn, "CALL selfserving_get_all_categories();") or show_custom_error(mysqli_error($iconn));
        $result = mysqli_store_result($iconn);
		while($row = mysqli_fetch_array($result))
		{
			$data[] = $row;
		}
        mysqli_free_result($result);
		if(mysqli_more_results($iconn))
		{
			mysqli_next_result($iconn);
		}
		return $data;
    }
    function selfserving_get_list_site()
	{
		$data = array();
		$iconn = $this->db->conn_id;
        mysqli_multi_query($iconn, "CALL selfserving_get_list_site();") or show_custom_error(mysqli_error($iconn));
        $result = mysqli_store_result($iconn);
        while($row = mysqli_fetch_array($result))
		{
			$data[] = $row;
		}
        mysqli_free_result($result);
		if(mysqli_more_results($iconn))
		{
			mysqli_next_result($iconn);
		}
		return $data;
	}
    function selfserving_get_url_by_bannerid($page,$rownum,$lstzone)
	{
	    $data=array();
		$data['url'] = array();
        $data['totalpage'] = array();
        $iconn = $this->db->conn_id;
        mysqli_multi_query($iconn, "CALL selfserving_get_url_by_bannerid_v2('$page','$rownum','$lstzone');") or show_custom_error(mysqli_error($iconn));
		$result = mysqli_store_result($iconn);
        while($row = mysqli_fetch_array($result))
		{
           $row['url']=trim($row['url'], "/" );
           $pos = strpos($row['url'], ':/');
            if ($pos === false){
                $row['url']='http://'.$row['url'];
                $data['url'][] = $row;
            } else {
                $data['url'][] = $row;
            }
		}
        mysqli_free_result($result);
        mysqli_next_result($iconn);
        $result = mysqli_store_result($iconn);
        while($row1 = mysqli_fetch_array($result))
		{
			$data['totalpage'][] = $row1;
		}
        mysqli_free_result($result);
		if(mysqli_more_results($iconn))
		{
			mysqli_next_result($iconn);
		}
		return $data;
    }
    function selfserving_get_banner_chart_hour($lstbanner,$curdate,$type)
    {
        $data = array();
        $data['ban1']=array();
        $data['ban2']=array();
        $iconn = $this->db->conn_id;
		mysqli_multi_query($iconn, "CALL selfserving_get_banner_chart_hour('$lstbanner','$curdate','$type');") or show_custom_error(mysqli_error($iconn));
		$result = mysqli_store_result($iconn);
        while($row = mysqli_fetch_array($result))
		{
			$data['ban1'][] = $row;
		}
        mysqli_free_result($result);
		mysqli_next_result($iconn);
        $result = mysqli_store_result($iconn);
        while($row1 = mysqli_fetch_array($result))
		{
			$data['ban2'][] = $row1;
		}
        mysqli_free_result($result);
		if(mysqli_more_results($iconn))
		{
			mysqli_next_result($iconn);
		}
		return $data;
    }
    function selfserving_get_url_trackingbanner($url,$domain,$bannerid,$zoneid)
	{
		$data = array();
		$iconn = $this->db->conn_id;
		mysqli_multi_query($iconn, "CALL selfserving_get_url_trackingbanner('$url','$domain','$bannerid','$zoneid');") or show_custom_error(mysqli_error($iconn));
		$result = mysqli_store_result($iconn);
        while($row = mysqli_fetch_array($result))
		{
			$data[] = $row;
		}
        mysqli_free_result($result);
		if(mysqli_more_results($iconn))
		{
			mysqli_next_result($iconn);
		}
		return $data;
	}
    function selfserving_admin_get_lstzone_tracking_by_banner($bannerid,$location)
    {
        $data = array();
		$iconn = $this->db->conn_id;
		mysqli_multi_query($iconn, "CALL selfserving_admin_get_lstzone_tracking_by_banner('$bannerid');") or show_custom_error(mysqli_error($iconn));
		$result = mysqli_store_result($iconn);
        while($row = mysqli_fetch_array($result))
		{
			$data['ban1'][] = $row;
		}
        //mysqli_free_result($result);
//		mysqli_next_result($iconn);
//        $result = mysqli_store_result($iconn);
//        while($row1 = mysqli_fetch_array($result))
//		{
//			$data['ban2'][] = $row1;
//		}
//        mysqli_free_result($result);
//        mysqli_next_result($iconn);
//        $result = mysqli_store_result($iconn);
//        while($row2 = mysqli_fetch_array($result))
//		{
//			$data['ban3'][] = $row2;
//		}
//        mysqli_free_result($result);
		if(mysqli_more_results($iconn))
		{
			mysqli_next_result($iconn);
		}
		return $data;
    }
    function selfserving_check_permit_by_user($userid,$permitid,$groupid,$id,$typeid)
    {
        $data = array();
		$iconn = $this->db->conn_id;
		mysqli_multi_query($iconn, "CALL selfserving_check_permit_by_user('$userid','$permitid','$groupid','$id','$typeid');") or show_custom_error(mysqli_error($iconn));
		$result = mysqli_store_result($iconn);
        while($row = mysqli_fetch_array($result))
		{
			$data[] = $row;
		}
        mysqli_free_result($result);
		if(mysqli_more_results($iconn))
		{
			mysqli_next_result($iconn);
		}
		return $data;
    }
}
?>